package com.zcf.gameController.gameUtils;

import com.zcf.gameController.F_Websocket_s;
import com.zcf.pojo.Msg;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;

import java.util.*;
import java.util.stream.Collectors;
/**
 * @author ： jj
 * @date ：2019/11/20 9:29
 * @description :
 * @path : SgCountdownTimer.java.SgCountdownTimer
 */
public class SgRobsTimer extends Thread{

	private  Integer timer;

	private RoomsParam myRoom;
	private Msg msgs =null;
	private F_Websocket_s ws;
	private Map<String, Object> data = new HashMap<>();
	public SgRobsTimer(Integer timer, RoomsParam myRoom, F_Websocket_s ws){
		this.timer = timer;
		this.ws = ws;
		this.myRoom = myRoom;
	}
	@Override
	public void run() {
		while (timer >0 ) {
			if ( Thread.currentThread().isInterrupted() ) {
				System.out.println("线程终止");
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("抢庄倒计时"+timer);
			timer --;
			myRoom.setTimer(timer);
			List<UserBean> userBeans =myRoom.getUsers().values().stream().filter(userBean->userBean.getIsReady().equals(1)).collect(Collectors.toList());
			Map<Integer, List<UserBean>> collect = userBeans.stream().collect(Collectors.groupingBy(UserBean::getIsOwner));
			int a=0;
			int b=0;
			int c=0;
			if (collect.get(1)!=null){
				a=collect.get(1).size();
			}
			if (collect.get(2)!=null){
				b=collect.get(2).size();
			}
			if (collect.get(3)!=null){
				c=collect.get(3).size();
			}
			System.out.println("在座人数:"+userBeans.size()+"申请上庄人数:"+a+"抢庄人数:"+b+"不抢人数:"+c);
			if (userBeans.size()==(a+b+c)){
				timer=0;
			}
			if(timer <= 0){
				data.clear();
				System.out.println("倒计时结束！！！！");
				System.out.println("111111111111111111");
				if (!myRoom.getSitting().equals(1)) {
					if (a > 0) {
						if (a == 1) {
							myRoom.setIsUp(collect.get(1).get(0).getId());
						} else {
							Random rand = new Random();
							int random = rand.nextInt(collect.get(1).size());
							myRoom.setIsUp(collect.get(1).get(random).getId());
						}
					} else if (b > 0) {
						Random rand = new Random();
						int random = rand.nextInt(collect.get(2).size());
						myRoom.setIsUp(collect.get(2).get(random).getId());
					}
					if (myRoom.getIsUp()!=0) {
						Long userid = myRoom.getIsUp();
						UserBean userBean = myRoom.getUsers().get(userid + "");
						List<Integer> postions = myRoom.getPostions();
						Integer betMoney = userBean.getMoney() / postions.size();
						for (int i = 0; i < postions.size(); i++) {
							myRoom.getBetMap().put(i, betMoney);
						}
						//先用contains判断是否有该元素
						if (postions.contains(userid.intValue())) {
							int j = postions.indexOf(userid.intValue());
							Integer pstn = myRoom.getPostions().get(7);
							if (pstn!=0){
								UserBean userBean1 = myRoom.getUsers().get(pstn + "");
								userBean1.setPosition(j);
							}
							Collections.swap(postions, 7, j);
							userBean.setPosition(7);
						}
						data.put("sort",1);
						data.put("positions", myRoom.getPostions());
						data.put("robsUp", myRoom.getIsUp());
						data.put("isStart", myRoom.getIsStart());
						msgs = new Msg(200, "确认庄家", data, "robsEnd");
					}else {
						data.put("sort",0);
						data.put("isStart", myRoom.getIsStart());
						msgs = new Msg(200, "无庄局", data, "robsEnd");
					}
					try {
						ws.broadcast(msgs, myRoom);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				if (myRoom.getIsUp()!=0) {
					UserBean u1=myRoom.getUserBean(myRoom.getIsUp());
					List<Integer> postions = myRoom.getPostions();
					Integer betMoney =u1.getMoney()/(postions.size()-1);
					for (int i = 0; i < postions.size()-1; i++) {
						myRoom.getBetMap().put(i, betMoney);
					}
					myRoom.getBetMap().put(postions.size()-1,0);
				}
//--------------------------------------------下注接口-----------------------------------------------
				//myRoom.setTimer(120);
				myRoom.setIsStart(3);
				data.clear();
				Integer bettonTime =myRoom.getRoomType().get("bettonTime") ;
				data.put("bettonTime",bettonTime);
				data.put("isStart" , myRoom.getIsStart());
				if (myRoom.getIsUp()!=0) {
					data.put("isUp",1);
					data.put("roomAreaMoney",myRoom.getBetMap());
				}else {
					data.put("isUp",0);
				}

				try {
					ws.broadcast(new Msg(200,"开始下注", data,"bettonGame"), myRoom);
				} catch (Exception e) {
					e.printStackTrace();
				}
				myRoom.setTimer(bettonTime);
				SgBettonTimer bettonTimer=new SgBettonTimer(myRoom.getTimer(),myRoom, ws);
				bettonTimer.start();
				System.out.println("开启下注倒计时");
			}
		}
	}
}
